package com.yf.zzmusic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yf.zzmusic.common.PageInfo;
import com.yf.zzmusic.pojo.dto.favoriteCount.FavoriteDeleteDTO;
import com.yf.zzmusic.pojo.dto.favoriteCount.FavoritePageDTO;
import com.yf.zzmusic.pojo.entity.FavoriteCount;
import com.yf.zzmusic.mapper.FavoriteCountMapper;
import com.yf.zzmusic.pojo.vo.favoriteCount.FavoriteCountOfAlbumVO;
import com.yf.zzmusic.pojo.vo.favoriteCount.FavoriteCountOfMusicCollectVO;
import com.yf.zzmusic.pojo.vo.music.MusicTableVO;
import com.yf.zzmusic.service.IFavoriteCountService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 收藏数表。用于存储音乐、歌单和专辑的收藏数 服务实现类
 * </p>
 *
 * @author
 * @since 2024-03-19
 */
@Service
public class FavoriteCountServiceImpl extends ServiceImpl<FavoriteCountMapper, FavoriteCount> implements IFavoriteCountService {

  @Autowired
  FavoriteCountMapper favoriteCountMapper;

  @Override
  public boolean deleteFavoriteByQuery(FavoriteDeleteDTO deleteDTO) {
    QueryWrapper<FavoriteCount> queryWrapper=new QueryWrapper<>();
    queryWrapper.eq("type",deleteDTO.getType());
    queryWrapper.eq("target_id",deleteDTO.getTargetId());
    queryWrapper.eq("user_id",deleteDTO.getUserId());
    return remove(queryWrapper);
  }

  @Override
  public PageInfo<FavoriteCountOfMusicCollectVO> getFavoriteOfMusicCollect(FavoritePageDTO favoritePageDTO) {
    Integer pageNum = favoritePageDTO.getPageNum();
    Integer pageSize = favoritePageDTO.getPageSize();
    //判断pageNum与pageSize
    if (pageNum == null) {
      pageNum=1;
      favoritePageDTO.setPageNum(1);
    }
    if (pageSize == null) {
      pageSize = 10;
      favoritePageDTO.setPageSize(10);
    }
    //执行自定义查询,获取音乐列表
    if (pageNum >= 1) {
      favoritePageDTO.setPageNum((pageNum - 1) * pageSize);
    }
    //获取总数
    Integer totalCount=favoriteCountMapper.favoriteCountListMusicCollectCount(favoritePageDTO);
    List<FavoriteCountOfMusicCollectVO> musicTableVOList=favoriteCountMapper.favoriteCountListMusicCollect(favoritePageDTO);
    PageInfo<FavoriteCountOfMusicCollectVO> pageInfo = new PageInfo<>();
    pageInfo.setPageSize(pageSize);
    pageInfo.setPageNum(pageNum);
    pageInfo.setTotal(totalCount);
    pageInfo.setData(musicTableVOList);
    return pageInfo;
  }

  @Override
  public PageInfo<FavoriteCountOfAlbumVO> getFavoriteOfAlbum(FavoritePageDTO favoritePageDTO) {
    Integer pageNum = favoritePageDTO.getPageNum();
    Integer pageSize = favoritePageDTO.getPageSize();
    //判断pageNum与pageSize
    if (pageNum == null) {
      pageNum=1;
      favoritePageDTO.setPageNum(1);
    }
    if (pageSize == null) {
      pageSize = 10;
      favoritePageDTO.setPageSize(10);
    }
    //执行自定义查询,获取音乐列表
    if (pageNum >= 1) {
      favoritePageDTO.setPageNum((pageNum - 1) * pageSize);
    }
    //获取总数
    Integer totalCount=favoriteCountMapper.favoriteCountListMusicCollectCount(favoritePageDTO);
    List<FavoriteCountOfAlbumVO> musicTableVOList=favoriteCountMapper.favoriteCountListAlbum(favoritePageDTO);
    PageInfo<FavoriteCountOfAlbumVO> pageInfo = new PageInfo<>();
    pageInfo.setPageSize(pageSize);
    pageInfo.setPageNum(pageNum);
    pageInfo.setTotal(totalCount);
    pageInfo.setData(musicTableVOList);
    return pageInfo;
  }
}
